#ifndef __LLD_NIM_PORTING_H__
#define __LLD_NIM_PORTING_H__

#include <sys_config.h>
#include <retcode.h>
#include <types.h>
#include <osal/osal.h>
#include <api/libc/alloc.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <hal/hal_gpio.h>
#include <hld/hld_dev.h>
#include <hld/nim/nim_dev.h>
#include <hld/nim/nim.h>
#include <hld/nim/nim_tuner.h>
#include <bus/i2c/i2c.h>
#include <hld/dmx/dmx_dev.h>
#include <hld/dmx/dmx.h>
#include <api/libdiseqc/lib_diseqc.h>

#if 1
#define NIM_PRINTF(...)
#else
#define NIM_PRINTF   libc_printf
#endif

#define CHANNEL_CHANGE_ASYNC

// use mutex
#define NIM_MUTEX_CREATE	osal_semaphore_create
#define NIM_MUTEX_DELETE	osal_semaphore_delete

#define NIM_MUTEX_ENTER(priv)  \
	if(priv->ul_status.nim_s3501_sema != OSAL_INVALID_ID) \
	{ \
		do{ \
			priv->ul_status.ret= osal_semaphore_capture(priv->ul_status.nim_s3501_sema, 1000); \
			if(priv->ul_status.ret!= OSAL_E_OK) \
				{NIM_PRINTF("nim_s3501_sema[%d] capture timeout, retry...\n", \
					priv->ul_status.nim_s3501_sema);} \
		}while(priv->ul_status.ret != OSAL_E_OK); \
	}

#define NIM_MUTEX_LEAVE(priv) \
	if(priv->ul_status.nim_s3501_sema != OSAL_INVALID_ID) \
	{ \
		osal_semaphore_release(priv->ul_status.nim_s3501_sema); \
	}  

// use flag
#ifdef CHANNEL_CHANGE_ASYNC
#define NIM_FLAG_CREATE osal_flag_create
#define NIM_FLAG_DEL osal_flag_delete
#define NIM_FLAG_WAIT osal_flag_wait
#define NIM_FLAG_SET osal_flag_set
#define NIM_FLAG_CLEAR osal_flag_clear
#endif

extern void nim_comm_delay(UINT32 us);
extern void nim_comm_sleep(UINT32 ms);

#define comm_malloc 			MALLOC
#define comm_memset				MEMSET
#define comm_free				FREE
#define comm_delay				nim_comm_delay
#define comm_sleep				nim_comm_sleep
#define comm_memcpy				memcpy

#define S3501_ERR_I2C_NO_ACK	ERR_I2C_NO_ACK
#define T_CTSK					OSAL_T_CTSK

#endif

